package your.packag;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import DAO.MyData;
import DTO.ExpendDTO;
import DTO.FinancialDTO;
import android.app.Activity;
import android.app.ProgressDialog;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FinancialActivity extends Activity implements OnClickListener {
	List<Cookie> cookie;
	DefaultHttpClient postClient;
	Button incomeCreate;
	Button expendCreate;
	Button transferCreate;
	TextView incomeText;
	TextView expendText;
	TextView balanceText;
	ProgressDialog dialog;
	ExpendDTO expendDTO;
	FinancialDTO financialDTO;
	static String xml;
	static org.jsoup.nodes.Document doc;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			dialog.dismiss();
			LoadUI();

		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.financial);
		incomeCreate = (Button) findViewById(R.id.finanbuttonText1);

		expendCreate = (Button) findViewById(R.id.finanbuttonText2);

		transferCreate = (Button) findViewById(R.id.finanbuttonText3);
		incomeCreate.setOnClickListener(this);
		expendCreate.setOnClickListener(this);
		transferCreate.setOnClickListener(this);
		incomeText = (TextView) findViewById(R.id.textIncome);	
		expendText = (TextView) findViewById(R.id.textExpend);
		balanceText = (TextView) findViewById(R.id.textBalance);
		incomeText.setText("");
		expendText.setText("");
		balanceText.setText("");
		
		
		postClient = LoginActivity.postClient;
		// LoadUI();
		dialog = new ProgressDialog(this);


	}

	@Override
	protected void onResume() {
		super.onResume();		
		dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
		new Thread(new Runnable() {

			public void run() {
				LoadData();
				handler.sendEmptyMessage(0);
			}
		}).start();

	}

	public void LoadUI() {


		incomeText.setText(financialDTO.getIncome());
		expendText.setText(financialDTO.getExpend());
		balanceText.setText(financialDTO.getBalance());

	}

	public void LoadData() {

		try {
			HttpGet httpGet = new HttpGet(
					"http://smartwebvina.com/smartlive/index.php?option=com_mymonney");
			HttpResponse response = LoginActivity.postClient.execute(httpGet);
			if (response.equals(LoginActivity.response)) {

			}
			HttpEntity httpEntity = response.getEntity();
			xml = EntityUtils.toString(httpEntity);
			doc = Jsoup.parse(xml);
			MyData data = new MyData();
			expendDTO = data.readExpend(doc);
			financialDTO = data.readFinancial(doc);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;

		switch (v.getId()) {

		case R.id.finanbuttonText1:

			intent = new Intent(this, CreateIncomeActivity.class);
			startActivity(intent);
			break;
		case R.id.finanbuttonText2:
			intent = new Intent(this, CreateExpendActivity.class);
			startActivity(intent);
			break;
		case R.id.finanbuttonText3:
			intent = new Intent(this, CreateTransferActivity.class);
			startActivity(intent);
		}

	}

}
